package com.wh1200.mybatis.xmlless.config

import com.wh1200.mybatis.xmlless.enums.XmllessDbType
import com.wh1200.mybatis.xmlless.replaceSpace
import com.wh1200.xmlless.test.dao.ServerDAO
import org.junit.jupiter.api.DisplayName
import org.junit.jupiter.api.Test
import kotlin.reflect.jvm.javaMethod
import kotlin.test.assertContains
import kotlin.test.assertNotNull


/**
 *
 * Created by 吴昊 on 2018-12-09.
 *
 * @author 吴昊
 * @since 0.0.1
 */
class ServerDAOResolverTest : BaseResolverTest(
    ServerDAO::class.java
) {

  @Test
  @DisplayName("解析持久化类的字段映射")
  fun resolveColumns() {
    val sql = createQueryForMethod(
        ServerDAO::save.javaMethod!!
    ).sql
    assertNotNull(sql)
    if (dbType == XmllessDbType.MYSQL) {
      assertContains(sql, "`order`")
    } else if (dbType == XmllessDbType.POSTGRE_SQL) {
      assertContains(sql, "\"order\"")
    }
  }

  @Test
  @DisplayName("引用自身")
  fun findById() {
    val query = createQueryForMethod(ServerDAO::findById.javaMethod!!)
    val sql = query.sql
    assertNotNull(sql)
    assertContains(sql.replaceSpace(), "LEFT JOIN server parent_id_server")
    assert(!sql.contains("<where>"))
  }

  @Test
  fun findByName() {
    val method = ServerDAO::findByName.javaMethod!!
    val query = createQueryForMethod(method)
    val resultId = buildResultMapId(method)
    val resultMap = builderAssistant.configuration.resultMaps.find {
      it.id == resultId
    }
    assertNotNull(resultMap)
    resultMap.propertyResultMappings.forEach {
      println(it.column + "/" + it.property)
    }
  }

}
